//
//  HWOpenServiceGameCell.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/29.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWOpenServiceGameCell: UITableViewCell {
    
    var bgView:UIView!
    var gameImageView:UIImageView!
    var nameLabel:UILabel!
    var serviceLabel:UILabel!
    var starIamgeView:UIImageView!
    var scoreLabel:UILabel!
    var typeLabel:UILabel!
    var tagOneLabel:UILabel!
    var tagTwoLabel:UILabel!
    var tagThreeLabel:UILabel!
    var collectionView:UICollectionView!
    let collectionProtocol = HWOpenServiceInfoProtocol()
    var model:HWOpenServiceModel?{
        didSet {
            gameImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            nameLabel.text = model?.gname
            serviceLabel.text = (model?.time ?? "") + (model?.server_name ?? "")
            scoreLabel.text = model?.point
            typeLabel.text = model?.type
            
            tagOneLabel.isHidden = true
            tagTwoLabel.isHidden = true
            tagThreeLabel.isHidden = true
            if model?.biaoqian.count == 1 {
                tagOneLabel.isHidden = false
                tagOneLabel.text = model?.biaoqian[0]
            }else if model?.biaoqian.count == 2 {
                tagOneLabel.isHidden = false
                tagOneLabel.text = model?.biaoqian[0]
                tagTwoLabel.isHidden = false
                tagTwoLabel.text = model?.biaoqian[1]
            }else if model?.biaoqian.count == 3 {
                tagOneLabel.isHidden = false
                tagOneLabel.text = model?.biaoqian[0]
                tagTwoLabel.isHidden = false
                tagTwoLabel.text = model?.biaoqian[1]
                tagThreeLabel.isHidden = false
                tagThreeLabel.text = model?.biaoqian[2]
            }else {
                tagOneLabel.isHidden = true
                tagTwoLabel.isHidden = true
                tagThreeLabel.isHidden = true
            }
            
            collectionProtocol.images = model?.imgs
            collectionView.reloadData()
        }
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWOpenServiceGameCell) {
        let reUse:String = "HWOpenServiceGameCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWOpenServiceGameCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWOpenServiceGameCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 12*widthScale
        contentView.addSubview(bgView)
        
        gameImageView = UIImageView()
        gameImageView.backgroundColor = .clear
        gameImageView.layer.cornerRadius = 12*widthScale
        bgView.addSubview(gameImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#444444", font: 14*widthScale)
        bgView.addSubview(nameLabel)
        
        serviceLabel = HWLabel.createLabel(title: "", color: "#5879FF", font: 12*widthScale)
        serviceLabel.textAlignment = .right
        bgView.addSubview(serviceLabel)
        
        starIamgeView = UIImageView()
        starIamgeView.image = UIImage(named: "starImage")
        bgView.addSubview(starIamgeView)
        
        scoreLabel = HWLabel.createBoldLabel(title: "9.9", color: "#444444", font: 10*widthScale)
        bgView.addSubview(scoreLabel)
        
        typeLabel = HWLabel.createLabel(title: "类型", color: "#888888", font: 10*widthScale)
        bgView.addSubview(typeLabel)
        
        tagOneLabel = HWLabel.createLabel(title: "标签", color: "#F87706", font: 8*widthScale)
        tagOneLabel.backgroundColor = .colorWithHexString(hexString: "#FFEDDE")
        tagOneLabel.layer.cornerRadius = 2*widthScale
        tagOneLabel.textAlignment = .center
        tagOneLabel.layer.masksToBounds = true
        bgView.addSubview(tagOneLabel)
        
        tagTwoLabel = HWLabel.createLabel(title: "标签", color: "#EBAE38", font: 8*widthScale)
        tagTwoLabel.backgroundColor = .colorWithHexString(hexString: "#FFF4DE")
        tagTwoLabel.layer.cornerRadius = 2*widthScale
        tagTwoLabel.textAlignment = .center
        tagTwoLabel.layer.masksToBounds = true
        bgView.addSubview(tagTwoLabel)
        
        tagThreeLabel = HWLabel.createLabel(title: "标签", color: "#FF5D5D", font: 8*widthScale)
        tagThreeLabel.backgroundColor = .colorWithHexString(hexString: "#FFECEC")
        tagThreeLabel.layer.cornerRadius = 2*widthScale
        tagThreeLabel.textAlignment = .center
        tagThreeLabel.layer.masksToBounds = true
        bgView.addSubview(tagThreeLabel)
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.minimumLineSpacing = 6*widthScale
        flowL.minimumInteritemSpacing = 0
        flowL.scrollDirection = .horizontal

        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = .white
        collectionView.register(HWOpenServiceInfoImageCell.self, forCellWithReuseIdentifier: "HWOpenServiceInfoImageCell")
        bgView.addSubview(collectionView)
        
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-20*widthScale)
            make.top.equalTo(10*widthScale)
            make.bottom.equalToSuperview()
        }
        
        gameImageView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(20*widthScale)
            make.width.height.equalTo(52*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(6*widthScale)
            make.top.equalTo(gameImageView).offset(3*widthScale)
        }
        
        serviceLabel.snp.makeConstraints { make in
//            make.centerY.equalTo(nameLabel)
            make.top.equalTo(23*widthScale)
            make.left.equalTo(nameLabel.snp.right).offset(2*widthScale)
            make.right.equalTo(-19*widthScale)
        }
        
        starIamgeView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(6*widthScale)
            make.width.height.equalTo(8*widthScale)
        }
        
        scoreLabel.snp.makeConstraints { make in
            make.centerY.equalTo(starIamgeView)
            make.left.equalTo(starIamgeView.snp.right).offset(2*widthScale)
        }
        
        typeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(starIamgeView)
            make.left.equalTo(scoreLabel.snp.right).offset(2*widthScale)
        }
        
        tagOneLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(5*widthScale)
            make.height.equalTo(12*widthScale)
            make.height.equalTo(24*widthScale)
            make.bottom.equalTo(gameImageView)
        }
        
        tagTwoLabel.snp.makeConstraints { make in
            make.left.equalTo(tagOneLabel.snp.right).offset(4*widthScale)
            make.size.centerY.equalTo(tagOneLabel)
        }
        
        tagThreeLabel.snp.makeConstraints { make in
            make.left.equalTo(tagTwoLabel.snp.right).offset(4*widthScale)
            make.size.centerY.equalTo(tagOneLabel)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(gameImageView.snp.bottom).offset(10*widthScale)
            make.height.equalTo(136*widthScale)
            make.bottom.equalTo(-20*widthScale)
        }
        
//        nameLabel.setContentHuggingPriority(.required, for: .vertical)
        nameLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
